use dataset, clear

gen inft = .
replace inft = ft_dem if pid3 == "Democrat"
replace inft = ft_rep if pid3 == "Republican"

gen Br = .
gen Bd = .

replace servspend = -servspend + 8
cor govjobs govhealth servspend

gen govjobs2 = govjobs

foreach x in govjobs govhealth servspend aidtoblacks defensespending ideology {
	replace `x' = -1 if `x' < 4 & !mi(`x')
	replace `x' = 0 if `x' == 4 
	replace `x' = 1 if `x' > 4 & !mi(`x')
}
replace abortion = -1 if abortion == 1
replace abortion = 0 if abortion == 2
replace abortion = 1 if inlist(abortion, 3, 4)

cor govjobs govhealth servspend aidtoblacks defensespending ideology 

gen constraint = govjobs + govhealth + servspend + abortion + ///
	aidtoblacks + defensespending + ideology

su constraint
tab constraint pid3

replace ideology = -abs(constraint)

su ideology
replace ideology = (ideology - r(min)) / (r(max) - r(min))

tabulate year if !mi(ideology), gen(year_)
local j = 1
levelsof year if !mi(ideology), local(levels) 
foreach l of local levels {
	rename year_`j' year_`l'
	gen ideologyXyear_`l' = ideology*year_`l'
	if `j' > 1 local X `X' ideologyXyear_`l'
	local j = `j' + 1
}

levelsof year if !mi(ideology), local(levels)
foreach l of local levels {
 
	reg inft ideology `X' if  year == `l' & pid3 == "Republican" [aw=weight]
	replace Br = _b[ideology] if year == `l'
	
	reg inft ideology `X' if  year == `l' & pid3 == "Democrat" [aw=weight]
	replace Bd = _b[ideology] if year == `l'

}

collapse Br Bd, by(year pid3)

#delimit;

gr tw 
	(sc Bd year if pid3 == "Democrat")
	(fpfit Bd year if pid3 == "Democrat", lpat(dash))
	(sc Br year if pid3 == "Republican", msym(Oh) mfcol(white))
	(fpfit Br year if pid3 == "Republican", lpat(solid))
	,
		name(g1, replace) 
		plotregion(style(none))
		title("Coefficients")
		ytitle("Impact of moderation on rating")
		xtitle("Year")
		ylab(, angle(horiz))
		legend(
			order(
				1 "Democrats" 2 "Polynomial trend, Democrats"
				3 "Republicans" 4 "Polynomial trend, Republicans"
			)
		)
		;

#delimit cr

gr export "figureA9b.eps", replace

